/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package co.edu.unal.bioingenium.kbmed.knowledge.ontology.dao;

import co.edu.unal.bioingenium.kbmed.knowledge.ontology.OntologyMetaData;
import java.sql.SQLException;
import java.util.HashMap;
import java.util.Map;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author Alejandro
 */
public class KnowledgeSourceDAOFactory {

    private static Map<String, KnowledgeSourceDAO> KnowledgeSourceDAOMap = new HashMap<String, KnowledgeSourceDAO>();

    /**
     *
     * @param ontologyMetaData 
     * @return
     */
    public static KnowledgeSourceDAO getKnowledgeSourceDAO(OntologyMetaData ontologyMetaData) {
        if (KnowledgeSourceDAOMap.containsKey(ontologyMetaData.getIdentifier())) {
            return KnowledgeSourceDAOMap.get(ontologyMetaData.getIdentifier());
        } else {
            //TODO Validate by reflection the class inheritance from base DAO
            KnowledgeSourceDAO knowledgeSourceDAO;
            try {
                knowledgeSourceDAO = (KnowledgeSourceDAO) Class.forName(ontologyMetaData.getDAOClass().getName()).newInstance();
                knowledgeSourceDAO.initStatements();
                KnowledgeSourceDAOMap.put(ontologyMetaData.getIdentifier(), knowledgeSourceDAO);
                return knowledgeSourceDAO;
            } catch (ClassNotFoundException ex) {
                Logger.getLogger(KnowledgeSourceDAOFactory.class.getName()).log(Level.SEVERE, null, ex);
            } catch (InstantiationException ex) {
                Logger.getLogger(KnowledgeSourceDAOFactory.class.getName()).log(Level.SEVERE, null, ex);
            } catch (IllegalAccessException ex) {
                Logger.getLogger(KnowledgeSourceDAOFactory.class.getName()).log(Level.SEVERE, null, ex);
            } catch (SQLException ex) {
                Logger.getLogger(KnowledgeSourceDAOFactory.class.getName()).log(Level.SEVERE, null, ex);
            }
            return null;
        }

    }
}
